
//装饰器
console.log("------------装饰器--------------");

//类的装饰器

//装饰器
function addFrom(constructor: any) {

    console.log("1111111111111类装饰器（addFrom）1111111111111111");
    //constructor构造函数
    console.log(constructor);
    constructor.prototype.from = "中国";

}

//传参的装饰器
function addAddress(address: string) {

    console.log("22222222222类装饰器（addAddress）2222222222222");
    return function (constructor: any) {

        constructor.prototype.address = address;
        constructor.prototype.getAddress = function () {
            console.log(this.name + " " + this.address);
        }

    }

}

//方法装饰器
function getNameMethod(target: any, methodName: string, descriptor: PropertyDescriptor) {

    console.log("1111111111111方法装饰器1111111111111111");
    console.log("target", target);
    console.log("methodName", methodName);
    console.log("descriptor", descriptor);

    //descriptor属性描述符

    target.id = 10000;
    // target.prototype.id=20000;
    descriptor.value = function () {
        console.log("装饰的方法");

    }

    // descriptor.writable=true;
}

//检验电话号的方法装饰器
  function checkMobile(target: any, methodName: string, descriptor: PropertyDescriptor) {

         let fn=descriptor.value;
        descriptor.value=function (arg:string) {

            if (!arg) {
                console.log("请输入电话号");
                return;
            }
    
            else if (!(/^1[3456789]\d{9}$/.test(arg))) {
                console.log("电话号码格式不正确");
                return;
            }
            else{
             
                //call改变this指向
                fn.call(this,arg); //执行原先方法
            }

        }


    }
//属性装饰器
function linkNameValue(parmas:string){

    console.log("1111111111111属性 饰器1111111111111111");
    return function (target:any,propertyName:string) {

        console.log("-----属性装饰器----");
        console.log("target",target);
        console.log("propertyName",propertyName);

        target[propertyName]=parmas;

        // target.linkName=parmas;

    }
}

 //参数的装饰器

 function mobileDec(target:any,methodName:string,index:number) {


    console.log("1111111111111参数饰器1111111111111111");
    console.log("-----参数装饰器----");
    
    console.log("target",target);
    console.log("methodName",methodName);
    console.log("index",index);
    
    
 }

@addFrom
@addAddress("北京市昌平区")
class Company {
    name: string;
    mobile: string;
    @linkNameValue("张三")
    linkName:string | undefined
    constructor(name: string) {
        this.name = name;
        this.mobile = "";
        // this.linkName="";

    }


    @checkMobile
    setMobile(mobile: string) {

      
        this.mobile = mobile;

        console.log("给电话赋值");
        

    }
    @getNameMethod
    getName(): void {

        console.log("类里的方法");


    }
    setInfo(name:string,@mobileDec mobile:string){

        console.log("设置信息");
        this.name=name;
        this.mobile=mobile;

    }

}

let comapny: any = new Company("百度");

console.log(comapny.from);

console.log(comapny.address);

comapny.getAddress();


console.log(comapny.id);

// comapny.getName=function(){
//     console.log("外部重写了方法");

// }
//报错 因为方法装饰器  禁止了 外部重写 getName方法
comapny.getName();


comapny.setMobile("99999");

console.log(comapny.mobile);

console.log("联系人",comapny.linkName);








// @addAddress("北京市海淀区")
// class Company2{
//     name:string;
//     constructor(name:string){
//         this.name = name;

//     }

// }

// let company2:any=new Company2("百度");

// company2.getAddress();

